using System;
using System.Windows.Media.Animation;

namespace GameOverlayWPF.Services
{
    /// <summary>
    /// 动画配置类 - 企业级动画参数管理
    /// 提供高度可定制的动画配置，支持不同场景的优化
    /// </summary>
    public class AnimationConfig
    {
        #region 预设配置

        /// <summary>
        /// 极致性能配置 - 适用于游戏环境
        /// </summary>
        public static AnimationConfig UltraPerformance => new AnimationConfig
        {
            ShowDuration = 200,
            HideDuration = 150,
            ScaleFrom = 0.5,
            ScaleTo = 1.0,
            ScaleHideTo = 0.9,
            OpacityFrom = 0.0,
            OpacityTo = 1.0,
            ShowEasing = new QuadraticEase { EasingMode = EasingMode.EaseOut },
            HideEasing = new QuadraticEase { EasingMode = EasingMode.EaseIn },
            FrameRate = 60,
            AccelerationRatio = 0.2,
            DecelerationRatio = 0.8,
            EnableBlur = false,
            EnableShadow = false
        };

        /// <summary>
        /// 极致优雅配置 - 适用于展示环境
        /// </summary>
        public static AnimationConfig UltraElegant => new AnimationConfig
        {
            ShowDuration = 400,
            HideDuration = 300,
            ScaleFrom = 0.2,
            ScaleTo = 1.0,
            ScaleHideTo = 0.7,
            OpacityFrom = 0.0,
            OpacityTo = 1.0,
            ShowEasing = new BackEase { EasingMode = EasingMode.EaseOut, Amplitude = 0.4 },
            HideEasing = new CubicEase { EasingMode = EasingMode.EaseIn },
            FrameRate = 60,
            AccelerationRatio = 0.1,
            DecelerationRatio = 0.9,
            EnableBlur = true,
            EnableShadow = true,
            BlurRadius = 20,
            ShadowDepth = 5
        };

        /// <summary>
        /// 平衡配置 - 性能与优雅的完美平衡
        /// </summary>
        public static AnimationConfig Balanced => new AnimationConfig
        {
            ShowDuration = 280,
            HideDuration = 220,
            ScaleFrom = 0.3,
            ScaleTo = 1.0,
            ScaleHideTo = 0.8,
            OpacityFrom = 0.0,
            OpacityTo = 1.0,
            ShowEasing = new BackEase { EasingMode = EasingMode.EaseOut, Amplitude = 0.3 },
            HideEasing = new QuinticEase { EasingMode = EasingMode.EaseIn },
            FrameRate = 60,
            AccelerationRatio = 0.1,
            DecelerationRatio = 0.9,
            EnableBlur = false,
            EnableShadow = true,
            ShadowDepth = 3
        };

        /// <summary>
        /// 微妙配置 - 适用于频繁操作
        /// </summary>
        public static AnimationConfig Subtle => new AnimationConfig
        {
            ShowDuration = 150,
            HideDuration = 120,
            ScaleFrom = 0.8,
            ScaleTo = 1.0,
            ScaleHideTo = 0.95,
            OpacityFrom = 0.3,
            OpacityTo = 1.0,
            ShowEasing = new SineEase { EasingMode = EasingMode.EaseOut },
            HideEasing = new SineEase { EasingMode = EasingMode.EaseIn },
            FrameRate = 60,
            AccelerationRatio = 0.3,
            DecelerationRatio = 0.7,
            EnableBlur = false,
            EnableShadow = false
        };

        #endregion

        #region 基本动画参数

        /// <summary>
        /// 显示动画时长(毫秒)
        /// </summary>
        public int ShowDuration { get; set; } = 280;

        /// <summary>
        /// 隐藏动画时长(毫秒)
        /// </summary>
        public int HideDuration { get; set; } = 220;

        /// <summary>
        /// 起始缩放比例
        /// </summary>
        public double ScaleFrom { get; set; } = 0.3;

        /// <summary>
        /// 目标缩放比例
        /// </summary>
        public double ScaleTo { get; set; } = 1.0;

        /// <summary>
        /// 隐藏时的缩放比例
        /// </summary>
        public double ScaleHideTo { get; set; } = 0.8;

        /// <summary>
        /// 起始透明度
        /// </summary>
        public double OpacityFrom { get; set; } = 0.0;

        /// <summary>
        /// 目标透明度
        /// </summary>
        public double OpacityTo { get; set; } = 1.0;

        #endregion

        #region 缓动函数

        /// <summary>
        /// 显示动画缓动函数
        /// </summary>
        public IEasingFunction ShowEasing { get; set; } = new BackEase 
        { 
            EasingMode = EasingMode.EaseOut, 
            Amplitude = 0.3 
        };

        /// <summary>
        /// 隐藏动画缓动函数
        /// </summary>
        public IEasingFunction HideEasing { get; set; } = new QuinticEase 
        { 
            EasingMode = EasingMode.EaseIn 
        };

        #endregion

        #region 性能参数

        /// <summary>
        /// 动画帧率
        /// </summary>
        public int FrameRate { get; set; } = 60;

        /// <summary>
        /// 加速比例 (0.0-1.0)
        /// </summary>
        public double AccelerationRatio { get; set; } = 0.1;

        /// <summary>
        /// 减速比例 (0.0-1.0)
        /// </summary>
        public double DecelerationRatio { get; set; } = 0.9;

        #endregion

        #region 视觉效果

        /// <summary>
        /// 启用模糊效果
        /// </summary>
        public bool EnableBlur { get; set; } = false;

        /// <summary>
        /// 启用阴影效果
        /// </summary>
        public bool EnableShadow { get; set; } = true;

        /// <summary>
        /// 模糊半径
        /// </summary>
        public double BlurRadius { get; set; } = 15;

        /// <summary>
        /// 阴影深度
        /// </summary>
        public double ShadowDepth { get; set; } = 3;

        /// <summary>
        /// 阴影透明度
        /// </summary>
        public double ShadowOpacity { get; set; } = 0.3;

        #endregion

        #region 高级配置

        /// <summary>
        /// 变换原点X坐标 (0.0-1.0)
        /// </summary>
        public double TransformOriginX { get; set; } = 0.5;

        /// <summary>
        /// 变换原点Y坐标 (0.0-1.0)
        /// </summary>
        public double TransformOriginY { get; set; } = 0.5;

        /// <summary>
        /// 启用硬件加速
        /// </summary>
        public bool EnableHardwareAcceleration { get; set; } = true;

        /// <summary>
        /// 启用子像素渲染
        /// </summary>
        public bool EnableSubpixelRendering { get; set; } = true;

        /// <summary>
        /// 最大并发动画数量
        /// </summary>
        public int MaxConcurrentAnimations { get; set; } = 10;

        #endregion

        #region 动态优化

        /// <summary>
        /// 根据系统性能自动调整配置
        /// </summary>
        /// <param name="cpuUsage">CPU使用率 (0.0-1.0)</param>
        /// <param name="memoryUsage">内存使用率 (0.0-1.0)</param>
        /// <returns>优化后的配置</returns>
        public AnimationConfig OptimizeForPerformance(double cpuUsage, double memoryUsage)
        {
            var config = this.Clone();

            // 高CPU使用率时减少动画复杂度
            if (cpuUsage > 0.8)
            {
                config.ShowDuration = Math.Max(config.ShowDuration / 2, 100);
                config.HideDuration = Math.Max(config.HideDuration / 2, 80);
                config.FrameRate = 30;
                config.EnableBlur = false;
                config.EnableShadow = false;
            }
            else if (cpuUsage > 0.6)
            {
                config.ShowDuration = (int)(config.ShowDuration * 0.7);
                config.HideDuration = (int)(config.HideDuration * 0.7);
                config.FrameRate = 45;
                config.EnableBlur = false;
            }

            // 高内存使用率时简化效果
            if (memoryUsage > 0.8)
            {
                config.EnableBlur = false;
                config.EnableShadow = false;
                config.MaxConcurrentAnimations = 3;
            }

            return config;
        }

        /// <summary>
        /// 克隆配置
        /// </summary>
        /// <returns>配置副本</returns>
        public AnimationConfig Clone()
        {
            return new AnimationConfig
            {
                ShowDuration = this.ShowDuration,
                HideDuration = this.HideDuration,
                ScaleFrom = this.ScaleFrom,
                ScaleTo = this.ScaleTo,
                ScaleHideTo = this.ScaleHideTo,
                OpacityFrom = this.OpacityFrom,
                OpacityTo = this.OpacityTo,
                ShowEasing = this.ShowEasing,
                HideEasing = this.HideEasing,
                FrameRate = this.FrameRate,
                AccelerationRatio = this.AccelerationRatio,
                DecelerationRatio = this.DecelerationRatio,
                EnableBlur = this.EnableBlur,
                EnableShadow = this.EnableShadow,
                BlurRadius = this.BlurRadius,
                ShadowDepth = this.ShadowDepth,
                ShadowOpacity = this.ShadowOpacity,
                TransformOriginX = this.TransformOriginX,
                TransformOriginY = this.TransformOriginY,
                EnableHardwareAcceleration = this.EnableHardwareAcceleration,
                EnableSubpixelRendering = this.EnableSubpixelRendering,
                MaxConcurrentAnimations = this.MaxConcurrentAnimations
            };
        }

        #endregion
    }
}
